var bootbox_global;
var grid;
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    bootbox_global = bootbox;
    $(function () {


        var columns_select ={name_dataIndex :'name',value_dataIndex :'user_id'};
        var  grid_select = new Select_Grid("/user/GetListPaging",$("#userselect"),columns_select,"rows=100000");

        grid_select.removeflag = 0;
        grid_select.Init(function(){


            dataInit();

        });


        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
            }
            },
            //{ title: '种类id', dataIndex: 'type_id',},
            {title: '姓名', dataIndex: 'name',},
            {title: '体重', dataIndex: 'weight',},
            {title: '目标体重', dataIndex: 'target_weight',},
            {
                title: '日期', dataIndex: 'gender', dataSource: function (data, datarow, gridobj, current_column) {

                var obj = data[datarow];

                var value = obj.record_day;

                return value.substring(0,10);


            }
            },


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/user/getWeightListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "user_id=" + $("#userselect").val();

            return param;

        }

       // dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });


        $("#userselect").bind("change", function () {
            dataInit();
        });


        //新增
        $("#btn_add").bind("click", function () {


            var alertarray = {
                form: {api: "/user/addWeight", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "体重", type: "text", placeholder: "", verify: "empty", name: "weight", id: "weight"},
                    {title: "时间", type: "date", placeholder: "", verify: "empty", name: "record_day", id: "record_day"},
                    {title: "姓名", type: "select", id: "user_id", form_name: "user_id", copyid: "userselect"},
                    //{title: "图片", type: "img", placeholder: "", verify: "empty", name: "pic_url2", id: "upimgs2"},


                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });

    });


});